package com.slimbloody.controller;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import protobuf.FeatLowerPriceDrugInfos;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pb")
public class PbController {
  @PostMapping("/build")
  public void build() throws InvalidProtocolBufferException {
    FeatLowerPriceDrugInfos.LowerPriceDrugInfo.Builder drugInfoBuilder = FeatLowerPriceDrugInfos.LowerPriceDrugInfo.newBuilder();


    List<FeatLowerPriceDrugInfos.WholesaleInfo> wholesaleInfoList = new ArrayList<>();
    for (int i = 0; i < 8; ++i) {
      FeatLowerPriceDrugInfos.WholesaleInfo.Builder infoBuilder = FeatLowerPriceDrugInfos.WholesaleInfo.newBuilder();
      infoBuilder.setUnitPrice(0);
      infoBuilder.setWholesaleId(0);
      FeatLowerPriceDrugInfos.WholesaleInfo wholesaleInfo = infoBuilder.build();
      wholesaleInfoList.add(wholesaleInfo);
    }

    drugInfoBuilder.addAllWholesaleInfos(wholesaleInfoList);
    FeatLowerPriceDrugInfos.LowerPriceDrugInfo drugInfo = drugInfoBuilder.build();
    ByteString byteString = drugInfo.toByteString();

    FeatLowerPriceDrugInfos.LowerPriceDrugInfo lowerPriceDrugInfo = FeatLowerPriceDrugInfos.LowerPriceDrugInfo.parseFrom(byteString);
    List<FeatLowerPriceDrugInfos.WholesaleInfo> wholesaleInfosList = lowerPriceDrugInfo.getWholesaleInfosList();
    for (int i = 0; i < 8; ++i) {
      FeatLowerPriceDrugInfos.WholesaleInfo wholesaleInfo = wholesaleInfosList.get(i);
      System.out.println(wholesaleInfo);
    }
  }
}
